package com.kws.merchant.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kws.common.security.auth.AuthUtil;
import com.kws.merchant.domain.AfterSalesHistory;
import com.kws.merchant.domain.AfterSalesMalfunction;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.AfterSalesWarranty;
import com.kws.merchant.domain.po.AfterSalesMalfunctionPo;
import com.kws.merchant.domain.vo.AfterSalesWarrantyVo;
import com.kws.merchant.mapper.AfterSalesMalfunctionMapper;
import com.kws.merchant.service.AfterSalesHistoryService;
import com.kws.merchant.service.AfterSalesMalfunctionService;
import com.kws.merchant.service.MerchantService;
import com.kws.merchant.util.FileUtil;
import com.kws.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 售后-故障单(AfterSalesMalfunction)表服务实现类
 *
 * @author yxy
 * @since 2024-10-22 19:23:10
 */
@Service("afterSalesMalfunctionService")
public class AfterSalesMalfunctionServiceImpl extends ServiceImpl<AfterSalesMalfunctionMapper, AfterSalesMalfunction> implements AfterSalesMalfunctionService {

    @Resource
    private AfterSalesMalfunctionMapper afterSalesMalfunctionMapper;
    @Autowired
    private AfterSalesHistoryService afterSalesHistoryService;

    /**
     * 分页查询列表
     *
     * @param afterSalesMalfunctionPo
     * @return
     */
    @Override
    public Page<AfterSalesMalfunction> pageList(AfterSalesMalfunctionPo afterSalesMalfunctionPo) {
        Integer pageSize = afterSalesMalfunctionPo.getPageSize();
        Integer totalPage = afterSalesMalfunctionPo.getTotalPage();
        String malfunctionNo = afterSalesMalfunctionPo.getMalfunctionNo();//故障单号
        String malfunctionType = afterSalesMalfunctionPo.getMalfunctionType();//故障类型
        String type = afterSalesMalfunctionPo.getType();//0设备 1商品服务
        String status = afterSalesMalfunctionPo.getStatus();
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        //查询是否为商家
        boolean flag = sysUser.getCompanyId()!=null;
        String sql = " ";
        if (flag) {
            sql = " a.merchant_id=" + sysUser.getCompanyId();
        }
        if (StringUtils.isNotEmpty(malfunctionNo)){
            sql+=" and a.malfunction_no='"+malfunctionNo+"'";
        }
        if (StringUtils.isNotEmpty(malfunctionType)){
            sql+=" and a.malfunction_type="+malfunctionType;
        }
        if (StringUtils.isNotEmpty(type)){
            sql+=" and a.type="+type;
        }
        if (StringUtils.isNotEmpty(status)){
            sql+=" and a.status="+status;
        }
        int total = afterSalesMalfunctionMapper.countToTal(sql);
        int sizeNum = (pageSize - 1) * totalPage;//计算分页
        //查询签名数据
        List<AfterSalesMalfunction> records=afterSalesMalfunctionMapper.getRecords(sql,sizeNum,totalPage);
        Page<AfterSalesMalfunction> page = new Page<>();
        page.setRecords(records);
        page.setTotal(total);
        page.setCurrent(pageSize);
        page.setPages((total+totalPage-1)/totalPage);
        page.setSize(totalPage);
        return page;
    }

    @Override
    public AfterSalesMalfunction detailById(Long id) {
        AfterSalesMalfunction afterSalesMalfunction = afterSalesMalfunctionMapper.detailById(id);
        if (ObjectUtils.isEmpty(afterSalesMalfunction)){
            throw new RuntimeException("无法查询该故障单");
        }
        String malfunctionNo = afterSalesMalfunction.getMalfunctionNo();
        afterSalesMalfunction.setImg(FileUtil.fixFileName()+afterSalesMalfunction.getImg());
//        List<AfterSalesHistory> warrantyHistoryList = afterSalesHistoryService.listByWarrantyNo(malfunctionNo);
//        AfterSalesWarrantyVo afterSalesWarrantyVo = new AfterSalesWarrantyVo();
//        afterSalesWarrantyVo.setAfterSalesInfo(afterSalesMalfunction);
//        afterSalesWarrantyVo.setWarrantyHistoryList(warrantyHistoryList);
        return afterSalesMalfunction;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> idList) {
//        afterSalesHistoryService.removeByAfterSalesNo("GZ",idList);
        this.removeByIds(idList);
        return true;
    }

    /**
     * 处理故障单
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean dispose(AfterSalesMalfunction afterSalesMalfunction) {
        afterSalesMalfunction.setImg(FileUtil.subFileName(afterSalesMalfunction.getImg()));
        afterSalesMalfunction.setStatus(1);
        return this.updateById(afterSalesMalfunction);
    }

    private AfterSalesMalfunction getOneByWarrantyNo(String afterSalesNo) {
        return this.getOne(Wrappers.<AfterSalesMalfunction>lambdaQuery().eq(AfterSalesMalfunction::getMalfunctionNo,afterSalesNo));
    }
}
